var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}).
      when('/add-user', {templateUrl: 'add-new.html', controller: AddCustCtrl}).
      when('/deletecust', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}).
      //when('/editcust/:custId', {templateUrl: 'update-cust.html', controller: ListCustCtrl}).
    //  when('/editcust/:custId', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}). 
        when('/editcust/', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}). 
      when('/updtcust/', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}). 
      when('/cancel/', {templateUrl: 'customer_search_ajs.html', controller: ListCustCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () {
   //  $window.alert("Hello Details  "+custid);
    this.get = function (custid,$window) {
        for (i in customermast) {
            if (customermast[i].custid == custid) {
                 $window.alert("Edit Customer Details  "+custid);
                return customermast[i];
            }
        }
 
    };
}
);


function ListCustCtrl($scope, $http,$window,ContactService,$location)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 15;
    $http.get('customerdata.php').success(function(data) {
    $scope.customermast = data;
    });
    
     
    
    $scope.edit = function(custid) {
     // $scope.master = {};
 //   $window.alert("Edit Customer Details  "+custid);
        //search customerid with given id and update it
       // var i =0;
          for(i in $scope.customermast) {
          // $window.alert("outer value of i is "+i);
            if($scope.customermast[i].custid == custid)
            {
              //   $window.alert(" value of custid is "+i+ "  "+custid);
               //we use angular.copy() method to create copy of original object
               $scope.newcustomermast = angular.copy($scope.customermast[i]);
            }
        }
         
       /*  $scope.customermast = {};
       
       $scope.reset();
        $scope.reset = function()
            {
               $window.alert("Reset  "+custid);
              $scope.customermast = angular.copy($scope.master);
            }; */
    };
    
      
   $scope.deletecust = function(item) {
      //$window.alert("Deletion Flow"+item);
        if (confirm('Do you really want to delete this record?')) 
        {
            $window.alert(item+' Record is being Deleted');
            $http.post('customerdelete_ajs.php',{'name': item}).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('customer_search_ajs.php#/editcust/');
                    window.location.href = '#/editcust/';
            });
                //window.location.href = 'customer_search_ajs.php';
        }
      };
      
    $scope.updtcustdata = function(newcustomermast) 
  {   
     $window.alert("Updation of Record for "+newcustomermast.prefx +","+newcustomermast.Name + ","+ newcustomermast.Address + ","+ newcustomermast.city + ","+ newcustomermast.custtype+ ","+ newcustomermast.email+ ","+ newcustomermast.phoneno+ ","+ newcustomermast.appadmchrg+ ","+ newcustomermast.custid );
     
 var sql = 'customer_search_updatedata.php?'+'&name='+newcustomermast.Name+'&prefx='+newcustomermast.prefx +'&address='+newcustomermast.Address+'&city='+newcustomermast.city+'&email='+newcustomermast.email+'&phoneno='+newcustomermast.phoneno+'&custid='+newcustomermast.custid+'&custtype='+newcustomermast.custtype+'&appadmchrg='+newcustomermast.appadmchrg;
	// $window.alert(sql);
	 
     $http.post(sql).success(function(data)  
   {
     $window.alert( 'Record for '+ newcustomermast.prefx  + newcustomermast.Name + 'got Successfully Updated');
     window.location.href = 'customer_search_ajs.php#/editcust/';
   });
  };
      
        $scope.cancel= function()
    {
        $scope.activePath = null;
     //$window.alert( 'Cancel ');
      $scope.activePath = $location.path('customer_search_ajs.php#/editcust/');
     window.location.href = '#/editcust/';
    };
      
 }
    
           
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
        
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        
        
 function AddCustCtrl($scope, $http, $location,$window) 
   {
      $scope.master = {};
      $scope.activePath = null;
      $scope.add_new = function(cust,AddNewForm) {
       console.log(cust);
   //   $window.alert(" value to insert are custid is "+prefx+ "  "+custid+" "+Name+" " +Address+" "+city+" "+email+" "+phoneno+" "+appadmchrg);
          
      // $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';    

		//var sql = 'customer_search_insertdata.php?'+'&name='+newcustomermast.Name+'&prefx='+newcustomermast.prefx +'&address='+newcustomermast.Address+'&city='+newcustomermast.city+'&email='+newcustomermast.email+'&phoneno='+newcustomermast.phoneno+'&custid='+newcustomermast.custid+'&custtype='+newcustomermast.custtype+'&appadmchrg='+newcustomermast.appadmchrg;
//		$window.alert(sql);
  //      $http.post(sql).success(function()
     
$http.post('savecustomer_ajs.php', cust).success(function()
          {
 $window.alert( 'Record got Successfully Added');        
            $scope.reset();
            $scope.activePath = $location.path('customer_search_ajs.php#/editcust/');
            window.location.href = '#/editcust/';
           });
          
      
        $scope.reset = function() 
        {
           $scope.newcustomermast = angular.copy($scope.master);
        };
        $scope.reset();
      };
  
      $scope.prefx = [{ id: 1, name: 'Mr'}, { id: 2, name: 'M/s'}, { id: 3, name: 'Mrs'}];
      $scope.custcat = [{ id: 1, name: 'CUSTOMER'}, { id: 2, name: 'DISTRIBUTOR'}, { id: 3, name: 'AGENT'} , { id: 4, name: 'CORPORATE'}, { id: 5, name: 'VENDOR'}];
    }


ctrl.directive('handleDestroy', function() {
    return function(scope, tElement, attributes) {        
        scope.$on('$destroy', function() {
            alert("In destroy of:" + scope.todo.text);
        });
    };
});


